﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace ADO.NET_WinFrom
{
    public partial class login : Form
    {
        string constr = ConfigurationManager.ConnectionStrings["Usersconn"].ConnectionString;

        public login()
        {
            InitializeComponent();
        }

        private void Login_button_Click(object sender, EventArgs e)
        {
            //获取textbox输入框值  Trim（)方法去掉值尾部空格
            string uname = this.textBox_uname.Text.Trim();
            string upwd = this.textBox_pwd.Text.Trim();
            if (uname.Length<=0 || upwd.Length<=0)
            {
                MessageBox.Show("用户名，密码不能为空");
                return;
            }

            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                string strsql = "SELECT UID FROM userInfo WHERE uname=@uname AND upwd=@upwd";
                SqlCommand cmd = new SqlCommand(strsql,conn);
                cmd.Parameters.AddWithValue("@uname", uname);
                cmd.Parameters.AddWithValue("@upwd", upwd);

                //ExecuteNonQuery()在执行 update、insert 和 delete 语句，返回值为该命令所影响的行数
                //ExecuteNonQuery()在执行select语句，不返回影响的行数，而是 - 1，不论查询的结果有没有值都会返回 - 1
                
                if (cmd.ExecuteScalar()!=null)
                { 
                    MessageBox.Show("登陆成功");
                    conn.Close();
                    index ind = new index();
                    ind.Show();
                }
                else
                {
                    MessageBox.Show("登陆失败，请检查用户名是否正确");
                }
                
            }
           
            
        }
    }
}
